﻿Imports Mystery
Imports System.IO

Public Class FileContentImageView
    Implements IPropertyControl


    Private _property_info As System.Reflection.PropertyInfo
    Private _content_property As ContentReference

    Private _ref As FileContent


    Public Property property_info As System.Reflection.PropertyInfo Implements IPropertyControl.property_info
        Get
            Return _property_info
        End Get
        Set(value As System.Reflection.PropertyInfo)
            _content_property = value.getMysteryAttribute(Of ContentReference)()
            _label = _content_property.label
            _property_info = value
        End Set
    End Property


    Private _content_button As Button

    Public Property value As Object Implements IPropertyControl.value
        Get
            Return _ref
        End Get
        Set(value As Object)
            If TypeOf (value) Is SingleContentReference(Of FileContent) Then value = DirectCast(value, ContentPropertyValue).getValue
            Dim new_value As FileContent = value
            If new_value Is Nothing AndAlso _ref Is Nothing Then Exit Property
            If new_value IsNot Nothing AndAlso new_value.Equals(_ref) Then Exit Property
            _ref = new_value
            Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
            'interfance
            my_image.Visibility = If(_ref Is Nothing, Windows.Visibility.Hidden, Windows.Visibility.Visible)
            If _ref IsNot Nothing Then
                my_image.Source = New BitmapImage(New Uri(cd.getFile(_ref).FullName, UriKind.RelativeOrAbsolute))
                my_image.Height = 400
                my_image.Width = 300
            End If

            RaiseEvent valueChanged()
        End Set
    End Property



    Private _content As IContent

    Public Property content_item As Mystery.IContent Implements IPropertyControl.content
        Get
            Return _content
        End Get
        Set(value As Mystery.IContent)
            _content = value
            Me.value = _content_property.retrive()(_content)
        End Set
    End Property


    Private _label As String
    Public ReadOnly Property label As String Implements IPropertyControl.label
        Get
            Return _label
        End Get
    End Property


    Public ReadOnly Property wished_height As System.Windows.GridLength Implements IPropertyControl.wished_height
        Get
            Return GridLength.Auto
        End Get
    End Property

    Public Function setInContent() As IEnumerable(Of IContent) Implements IPropertyControl.setInContent
        Return New List(Of IContent)
    End Function

    Private Sub UserControl_LayoutUpdated(sender As System.Object, e As System.EventArgs) Handles MyBase.LayoutUpdated
        If my_image.Width > Me.Width Then my_image.Width = Me.Width
        If my_image.Height > Me.Height Then my_image.Height = Me.Height
    End Sub

    Public Event valueChanged() Implements IPropertyControl.valueChanged
End Class
